package com.softserveinc.interactive_board.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.softserveinc.interactive_board.domain.User;
import com.softserveinc.interactive_board.service.InteractiveBoardService;

@Controller
@RequestMapping("/administration")
public class AdministrationController {

	
	@RequestMapping(method = RequestMethod.GET)
	public String def() {
		return "redirect:/administration/";
	}
	
	
	@RequestMapping("/")
	public String index() {
		return "administration/index";
	}
	
	
	@RequestMapping("/users")
	public String users() {
		return "redirect:/administration/users/";
	}
	
	
	@RequestMapping("/users/")
	public String users(Model model) {
		model.addAttribute("users", interactiveBoardService.getAllUsers());
		return "administration/users/index";
	}

	
	//----------------------------Add-------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/users/add")
	public String usersAdd(Model model) {
		model.addAttribute("userAttr", new User());
		model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
		return "administration/users/add";
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/users/add")
	public String usersAdd(@ModelAttribute("userAttr") User user) {
		this.interactiveBoardService.addUser(user);
		return "redirect:/administration/users/";
	}
	
	//----------------------------Edit------------------------------------
	@RequestMapping(method = RequestMethod.GET, value = "/users/edit")
	public String usersEdit(@RequestParam("user_id") Long userId, Model model) {
		List<User> list = this.interactiveBoardService.getUser(userId);
		if (list.isEmpty())
			return "redirect:/administration/users";
		model.addAttribute("userAttr", list.get(0));
		model.addAttribute("userTypesAttr", this.interactiveBoardService.getRegistratebleUserTypes());
		return "administration/users/edit";
	}
	
	@RequestMapping(method = RequestMethod.POST, value = "/users/edit")
	public String usersEdit(@ModelAttribute("userAttr") User user) {
		this.interactiveBoardService.updateUser(user);
		return "redirect:/administration/users";
	}
	
	
	//----------------------------Delete-------------------------------------
	@RequestMapping("/users/delete")
	public String usersDelete(@RequestParam("user_id") Long userId) {
		this.interactiveBoardService.deleteUser(userId);
		return "redirect:/administration/users";
	}


	private InteractiveBoardService interactiveBoardService;
	
	@Autowired
	@Qualifier("interactiveBoardServiceImpl")
	public void setInteractiveBoardService(InteractiveBoardService interactiveBoardService) {
		this.interactiveBoardService = interactiveBoardService;
	}
	
}
